home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: STL: for_each vs transform
- Date: 1 Mar 1996 18:12:44 GMT
- Organization: Borland International
- Message-ID: <4h7ems$6v@druid.borland.com>
- References: <3135F631.41C6@austin.ibm.com>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <3135F631.41C6@austin.ibm.com>, leou@austin.ibm.com says...
- >
- >How can iterate thru a container, using for_each, and apply a function
- >object which modifies each of the elements in the container?
- >I tried passing the argument by name, but the template definition
- >will not allow it.
- >
- >Here's the example: Note addr and size values never change.
- >
- >#include <iostream.h>
- >#include <algo.h>
- >#include <vector.h>
- >
- >struct S { vector<int> v; };
- >struct g : public binary_function<S,int,int> {
- > int operator()(S s,int x) const
- > {s.v.push_back(x);
- > cout<<"x="<<x<<endl;
- > cout<<"g() addr of s:"<<(size_t)&s<<" size of v:"<<s.v.size()<<endl;
- > return 0;};
- >};
- >
- >void main() {
- >vector<S> A(3); // A has 3 vector<int>'s
- >for (int i=0; i<2; i++)
- > for_each( A.begin(),A.end(), bind2nd(g(),i) );
- >}
-
- I made three changes in the first few lines of this code. None of them affect
- the legality of the call to for_each. With those changes, this code compiled
- just fine with BC++ 5.0. Here are the changed lines:
-
- #include <iostream.h>
- #include <algorothm> // 1: ANSI/ISO header name
- #include <vector> // 2: ANSI/ISO header name
-
- using namespace std; // 3: these things live in namespace std
-
-